#include "queue.h"

void init_queue(queue *q, int size)
{
	q->size = size;
	q->q = (int *) malloc(q->size * sizeof(int));
    q->first = 0;
	q->last = q->size-1;
    q->count = 0;
}

int enqueue(queue *q, int x)
{
	if (q->count >= q->size)
		return -1;
    else {
            q->last = (q->last+1) % q->size;
            q->q[ q->last ] = x;    
            q->count = q->count + 1;
    }
	return 0;
}

int dequeue(queue *q)
{
        int x = -1;

        if (q->count <= 0) 
			;
        else {
                x = q->q[ q->first ];
                q->first = (q->first+1) % q->size;
                q->count = q->count - 1;
        }

        return x;
}

int empty(queue *q)
{
        if (q->count <= 0) return 1;
        else return 0;
}